package com.tencreat.mall.controller.admin;

import cn.hutool.json.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dtflys.forest.annotation.Get;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.AjaxResult;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.TenCreatUtil;
import com.tencreat.common.utils.poi.ExcelUtil;
import com.tencreat.domain.mall.MallAftersale;
import com.tencreat.domain.mall.MallDeliverAddress;
import com.tencreat.domain.mall.dto.AftersaleAuditDTO;
import com.tencreat.domain.mall.dto.MallAdminAfterSaleDTO;
import com.tencreat.domain.mall.dto.MallShipAddressDTO;
import com.tencreat.domain.mall.vo.MallAdminAftersaleVO;
import com.tencreat.domain.mall.vo.MallAfterSaleCountVO;
import com.tencreat.domain.mall.vo.MallRefundCountVO;
import com.tencreat.service.mall.IMallAftersaleService;
import com.tencreat.service.mall.IMallDeliverAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 售后Controller
 *
 * @author tencreat
 * @date 2021-08-15
 */
@Api(tags="售后api")
@RestController
@RequestMapping("/mall/aftersale")
public class AdminMallAftersaleController extends BaseController {
    @Autowired
    private IMallAftersaleService mallAftersaleService;

    @Autowired
    private IMallDeliverAddressService mallDeliverAddressService;

    /**
     * 查询售后列表
     */
    @ApiOperation(value="查询售后列表")
    @PreAuthorize("@ss.hasPermi('mall:aftersale:list')")
    @PostMapping("/list")
    public HttpResult<IPage<MallAdminAftersaleVO>> list(@RequestBody MallAdminAfterSaleDTO dto) {
        return HttpResult.ok(mallAftersaleService.pages(generatePage(dto), dto));
    }

    /**
     * 导出售后列表
     */
    @ApiOperation(value="导出售后列表")
    @PreAuthorize("@ss.hasPermi('mall:aftersale:export')")
    @Log(title = "订单管理>售后管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(@RequestBody MallAdminAfterSaleDTO dto) {
        IPage<MallAdminAftersaleVO> iPage = mallAftersaleService.pages(getMaxPage(), dto);
        iPage.getRecords().forEach(item -> {
            if (item.getAmount() == null){
                item.setAmount(BigDecimal.ZERO);
            }
        });
        ExcelUtil<MallAdminAftersaleVO> util = new ExcelUtil<>(MallAdminAftersaleVO.class);
        return util.exportExcel(iPage.getRecords(), "售后列表");
    }

    /**
     * 获取售后详细信息
     */
    @ApiOperation(value="获取售后详细信息")
    @PreAuthorize("@ss.hasPermi('mall:aftersale:query')")
    @PostMapping(value = "/getInfo")
    public HttpResult<MallAftersale> getInfo(@RequestBody   ReqIdParam id) {
        return HttpResult.ok(mallAftersaleService.selectMallAftersaleById(id));
    }

    /**
     * 审核售后
     */
    @ApiOperation(value="审核售后")
    @PreAuthorize("@ss.hasPermi('mall:aftersale:edit')")
    @Log(title = "订单管理>售后管理", businessType = BusinessType.AUDIT)
    @PostMapping(value = "/edit")
    public HttpResult<Boolean> edit(@RequestBody AftersaleAuditDTO dto) {
        return to(mallAftersaleService.updateMallAftersale(dto));
    }

    @ApiOperation(value="售后拒绝")
    @PreAuthorize("@ss.hasPermi('mall:aftersale:edit')")
    @Log(title = "订单管理>售后管理", businessType = BusinessType.AUDIT)
    @PostMapping(value = "/refuse")
    public HttpResult<Boolean> refuse(@RequestBody  AftersaleAuditDTO dto) {
        return to(mallAftersaleService.refuseMallAftersale(dto));
    }

    @ApiOperation(value="退款退货审核")
    @PreAuthorize("@ss.hasPermi('mall:aftersale:edit')")
    @Log(title = "订单管理>售后管理", businessType = BusinessType.AUDIT)
    @PostMapping(value = "/audit")
    public HttpResult<Boolean> auditMallAftersale(@RequestBody  AftersaleAuditDTO dto) {
        return to(mallAftersaleService.auditMallAftersale(dto));
    }

    /**
     * 删除售后
     */
    @ApiOperation(value="删除售后")
    @PreAuthorize("@ss.hasPermi('mall:aftersale:remove')")
    @Log(title = "订单管理>售后管理", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
    public HttpResult<Boolean> remove(@RequestBody  ReqIdsParam ids) {
        return to(mallAftersaleService.deleteMallAftersaleByIds(ids));
    }
    @ApiOperation("设置退货地址")
    @PostMapping("/ship-address")
    @Log(title = "订单管理>售后管理", businessType = BusinessType.INSERT)
    public HttpResult<Void> shipAddress(@RequestBody MallShipAddressDTO dto){
        dto.setUserId(getLoginCheck().getUser().getUserId());
        mallDeliverAddressService.shipAddress(dto);
        return HttpResult.ok();
    }

    @ApiOperation("获取退货地址")
    @GetMapping("/get-address")
    public  HttpResult<MallDeliverAddress> getAddress(){
        MallDeliverAddress mallDeliverAddress = mallDeliverAddressService.lambdaQuery().eq(MallDeliverAddress::getUserId, getLoginCheck().getUser().getUserId()).eq(MallDeliverAddress::getDeliverType, 1).one();
        return HttpResult.ok(mallDeliverAddress);
    }

    @ApiOperation("编辑退货地址")
    @PostMapping("/edit-address")
    @Log(title = "订单管理>售后管理", businessType = BusinessType.UPDATE)
    public HttpResult<Void> editAddress(@RequestBody MallShipAddressDTO dto){
        dto.setUserId(getLoginCheck().getUser().getUserId());
        mallDeliverAddressService.lambdaUpdate().set(MallDeliverAddress::getName,dto.getName())
                .set(MallDeliverAddress::getProvince,dto.getProvince())
                .set(MallDeliverAddress::getCity,dto.getCity())
                .set(MallDeliverAddress::getCounty,dto.getCounty())
                .set(MallDeliverAddress::getAddressDetail,dto.getAddressDetail())
                .set(MallDeliverAddress::getTel,dto.getTel())
                .eq(MallDeliverAddress::getUserId,dto.getUserId()).eq(MallDeliverAddress::getDeliverType,1).update();
        return HttpResult.ok();
    }

    @ApiOperation("售后管理类型汇总")
    @GetMapping("/afterSale-count")
    public HttpResult<MallAfterSaleCountVO> afterSaleCount(){
        return HttpResult.ok(mallAftersaleService.afterSaleCount());
    }

    @ApiOperation("退款金额汇总")
    @PostMapping("/refund-count")
    public HttpResult<MallRefundCountVO> refundCount(){
        return HttpResult.ok(mallAftersaleService.refundCount());
    }
}
